<template>
	<view>
		<van-address-list
			v-model="chosenAddressId"
			:list="list"
			default-tag-text="默认"
			:switchable="true"
			@select="onSelect"
			@add="onAdd"
			@edit="onEdit"
		  />
	</view>
</template>

<script>
	
	import { selectCustomerAddressList,updateCustomerAddrDefault } from '@/api/customer/customer.js'
	import { Dialog,Toast } from 'vant';
	export default {
		data() {
			return {
				chosenAddressId: "",
				list: [],
			}
		},
		created() {
		    this.getList();
		},
		methods: {
		  toBack() {
			//返回上一级页面
			this.$router.go(-1)
		  },
		  // 查询地址信息列表
		  getList(){
			selectCustomerAddressList().then((res) =>{
			  for (let i = 0; i < res.data.data.length; i++){
				this.list.push({ "id": null,"name":null,"tel":null,"address":null,"isDefault":null});
				this.list[i].id =  res.data.data[i].customerAddrId;
				this.list[i].name =  res.data.data[i].name;
				this.list[i].tel =  res.data.data[i].tel;
				this.list[i].address = res.data.data[i].province+res.data.data[i].city+res.data.data[i].county+res.data.data[i].addressDetail;
				if(res.data.data[i].isDefault == "1"){
				  this.list[i].isDefault = true
				  this.chosenAddressId = res.data.data[i].customerAddrId;
				}else{
				  this.list[i].isDefault = false
				}
			  }
			})
		  },
		  // 添加配送地址
		  onAdd() {
			  uni.navigateTo({
			  	url:'/pages/mine_sitmanage/address_edit?addressType=1'
			  })
		  },
		  // 修改配送地址
		  onEdit(item, index) {
			  uni.navigateTo({
			  	url:'/pages/mine_sitmanage/address_edit?addressType=2&customerAddrId='+item.id
			  })
			},
		  // 切换选中
		  onSelect(index,item){
			console.log("index:"+JSON.stringify(index)+"item:"+item);
			Dialog.confirm({
			  title: '默认地址',
			  message: '设为默认地址吗?',
			}).then(() => {
			  updateCustomerAddrDefault(index.id).then((res) => {
				if(res.data.code == 200){
				  Toast({
					message: "设置成功",
					position: "top"
				  });
				  uni.navigateBack();
				}
			  })
			  }).catch(() => {
	
			  });
			},
		}
	}
</script>

<style>

</style>
